/*
 * DA2Discoverer.java
 *
 * Created on 11 marzo 2007, 21.56
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.bloomingstars.bluepad.comm;

import java.io.InputStream;

/**
 *
 * @author Emmanuele Sordini
 */
public final class DA2Discoverer
{        
    public DA2Discoverer(DA2Address address)
    {
        this.address = address;
    }
    
    /*public void writeString(String s) throws IOException
    {       
       if (out != null)
       {
           System.out.println("Now writing: " + s);
           out.write(s.getBytes());
           out.flush();
       }
    }
    
    public void pause(long millis)
    {
        System.out.println("Starting " + ((float) millis / 1000.0) + "-second pause");
        try
        {
          Thread.sleep(millis);
        }
        catch (InterruptedException ie)
        {
            
        }
        
        System.out.println("Pause ended");
    }*/
    
  /*  public void execute()
    {
        try
        {
            LocalDevice localDevice = LocalDevice.getLocalDevice();
            DiscoveryAgent discoveryAgent = localDevice.getDiscoveryAgent();
            
            DA2DiscoveryListener listener = new DA2DiscoveryListener(this);
            
            System.out.println("Starting device inquiry...");
            discoveryAgent.startInquiry(DiscoveryAgent.GIAC, listener);
            
            // Wait for device discovery to end
            synchronized (this)
            {
                try
                {
                    wait();
                }
                catch (InterruptedException iex)
                {
                    iex.printStackTrace(System.err);
                }
            }
            System.out.println("Device inquiry ended...");
            
            List<RemoteDevice> remoteDevices = listener.getRemoteDevicesDiscovered();
            
            Iterator<RemoteDevice> it = remoteDevices.iterator();

            if (it.hasNext())
            {
                RemoteDevice remoteDevice = it.next();
                                
                
                System.out.println("Starting service search...");
                UUID[] uuids = new UUID[1];
                uuids[0] = new UUID(0x0003); // RFCOMM
                int[] attrSet = { 0, 1, 2, 3, 4, 256 };
                int transId = discoveryAgent.searchServices(attrSet, uuids, remoteDevice, listener);
                
                // Wait for service discovery to end
                synchronized (this)
                {
                    try
                    {
                        wait();
                    }
                    catch (InterruptedException iex)
                    {
                        iex.printStackTrace(System.err);
                    }
                }
                System.out.println("Service discovery ended...");
                
                // Service discovery ended. Try to open connection
                List<ServiceRecord> services = listener.getServicesDiscovered();
                Iterator<ServiceRecord> it1 = services.iterator();
                
            }                       
        }
        catch (BluetoothStateException e)
        {
            System.err.println("Problems while inquiring for devices");
            e.printStackTrace();
        }
    }*/
    
    private DA2Address address;
    
}

class DA2Reader implements Runnable
{
    InputStream in;
    
    public DA2Reader(InputStream in)
    {
        this.in = in;
    }
    
    public void run()
    {
        try
        {
            System.out.println("Receiver thread started");
            Thread.sleep(250);
            while (true)
            {                
                // byte[] buffer = new byte[256];
                
                if (in != null) // && in.available()>0)
                {           
                    int b = in.read();
                    System.out.println("Read: " + (char) b);
                }
                else
                {
                    System.out.println("Nothing to read");
                }
                
            }
        }
        catch (Exception ex)
        {
            System.err.println("Error in receiver thread");
            ex.printStackTrace();
        }
    }        
}
